package com.yunkaweilai.android.view.dialog.member;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.flyco.dialog.utils.CornerUtils;
import com.flyco.dialog.widget.base.BaseDialog;
import com.github.lazylibrary.util.ToastUtils;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.constants.CommonConstants;
import com.yunkaweilai.android.interfaces.MyDialogInterface;
import com.yunkaweilai.android.model.Event;
import com.yunkaweilai.android.model.member.MemberHeadModel;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.Utils;
import com.yunkaweilai.android.view.MyToast;

import org.greenrobot.eventbus.EventBus;

/**
 * 锁定挂失会员
 * Created by Administrator on 2016/12/22 0022.
 */

public class MemberStatusDialogForLock extends BaseDialog<MemberStatusDialogForLock> {
    ImageView idImgClose;
    private TextView idTvContent;
    private TextView idTvClose;
    private TextView idTvRight;
    private ImageView imageView;
    private MemberHeadModel model;
    private String other;
    private String content;
    private TextView views;
    private MyDialogInterface myDialogInterface;
    private String status;

    public MemberStatusDialogForLock(Context context,MyDialogInterface myDialogInterface, MemberHeadModel model, String other, String content, TextView views, String Status,ImageView imageView) {
        super(context);
        this.model = model;
        this.other = other;
        this.content = content;
        this.views = views;
        this.status = Status;
        this.myDialogInterface = myDialogInterface;
        this.imageView = imageView;
    }

    @Override
    public View onCreateView() {
        widthScale(0);
        //showAnim(new Swing());
        //dismissAnim(this, new ZoomOutExit());
        View inflate = View.inflate(mContext, R.layout.dialog_member_status, null);
        idImgClose = (ImageView) inflate.findViewById(R.id.id_img_close);
        idTvContent = ((TextView) inflate.findViewById(R.id.id_tv_content));
        idTvRight = ((TextView) inflate.findViewById(R.id.id_tv_right));
        idTvClose = (TextView) inflate.findViewById(R.id.id_tv_close);
        idTvContent.setText(content + "");
        //设置背景颜色，和背景的透明圆弧角度
        inflate.setBackgroundDrawable(
                CornerUtils.cornerDrawable(Color.parseColor("#f8f9fa"), dp2px(10)));

        return inflate;
    }

    @Override
    public void setUiBeforShow() {
        idImgClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        idTvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initHttp();
            }
        });
        idTvClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    private void initHttp() {
        HttpRequest.postUrl(Api.DO_LOCK_STATUS)
                .addParams("member_id", model.getId())
                .addParams("status", status)
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(mContext, json);
                        if (checkCode) {
                            MyToast.showShortToast(mContext, "操作成功");
                            if ("1".equals(status)) {
                                views.setText("未锁定");
                                imageView.setImageResource(R.mipmap.ic_lock_normal);
//                                views.setTextColor(mContext.getResources().getColor(R.color.white));
                                model.setMl_status("1");
                                myDialogInterface.RightClick();
                            } else {
                                views.setText("已锁定");
                                imageView.setImageResource(R.mipmap.ic_lock_lock);
//                                views.setTextColor(mContext.getResources().getColor(R.color.wjx_content_txt_black_left));
                                model.setMl_status("2");
                                myDialogInterface.CancelClick();
                            }
                        }
                        EventBus.getDefault().post(new Event.TypeEvent(CommonConstants.MEMBER_LIST, true));
                        dismiss();
                    }

                    @Override
                    public void onError(Exception e) {
                        ToastUtils.showToast(mContext, "系统错误");
                        dismiss();
                    }
                });
    }

    private void setImage() {
        if (model.getStatus_info().is_valid()) {//过期
            imageView.setImageResource(R.mipmap.ic_lock_valid);
        } else if (model.getMl_status().equals("1")) {//正常
            imageView.setImageResource(R.mipmap.ic_lock_normal);
        } else if (model.getMl_status().equals("2")) {//锁定
            imageView.setImageResource(R.mipmap.ic_lock_lock);
        } else if (model.getMl_status().equals("3")) {//挂失
            imageView.setImageResource(R.mipmap.ic_lock_lose);
        }

    }

}
